package com.jmz.apt.api.service;

import com.jmz.apt.api.dao.TygpGroupItemsDao;
import com.jmz.apt.api.entity.dbEntity.*;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.exception.BusinessException;
import com.jmz.apt.api.message.request.RequestMessage;
import com.jmz.apt.api.service.tpl.TygpGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TimeTplService {

    @Autowired
    TygpGroup tygpGroup;

    @Autowired
    TygpGroupItemsDao tygpGroupItemsDao;

    /**
     * @Description Find the group of template list .
     * @Author { Create by fanglexue on   2019/12/3 at 5:11 下午 }
     * @Path admin/
     **/
    public List<TimeTygpGroup> getTplGroupList(RequestMessage requestMessage) {
        List<TimeTygpGroup> lst = new ArrayList<TimeTygpGroup>();
        try {
            lst = tygpGroup.findAllTimeTplGroups(requestMessage);
        } catch (DataAccessException e) {
            log.error(e.getMessage());
            throw new BusinessException(ResponseCode.DB_OPERATION_ERROR.getCode(),
                    ResponseCode.DB_OPERATION_ERROR.getDesc());
        }
        return lst;
    }

    public Integer count(RequestMessage requestMessage) {
        return 100;
    }


    /**
     * @Description Batch add.
     * @Author { Create by fanglexue on   2019/12/3 at 5:40 下午 }
     * @Path admin/
     **/
    @Transactional
    public Boolean saveTimeTygpGroup(TimeTygpGroup timeTygpGroup){
        try {
            tygpGroup.saveGroup(timeTygpGroup);
        }catch (DataAccessException e){
            log.error(e.getMessage());
            throw new BusinessException(ResponseCode.DB_OPERATION_ERROR.getCode(),
                    ResponseCode.DB_OPERATION_ERROR.getDesc());
        }catch (BusinessException be){
            throw new BusinessException(be.getCode(),
                    be.getMessage());
        }
        return  true;
    }

    /**
     * @Description Batch update.
     * @Author { Create by fanglexue on   2019/12/3 at 5:39 下午 }
     * @Path admin/
     **/
    @Transactional
    public Boolean updateTimeTygpGroup(List<List<TimeTygpGroup>> timeTygpGroups){
        return true;
    }

    /**
    public void batchSaveTygpGroup(List<TimeTygpGroup> timeTygpGroups){
        for(TimeTygpGroup ttg : timeTygpGroups){
            List<TimeTygpGroupItems>  tts = ttg.getTtgItems();
            if(!tts.isEmpty() && tts != null){
                for (TimeTygpGroupItems tt : tts){
                    tygpGroupItemsDao.saveGroupItems(tt);
                }
            }
        }
    }

    public void batchUpdateTygpGroup(List<TimeTygpGroup> timeTygpGroups){
        for(TimeTygpGroup ttg : timeTygpGroups){
            List<TimeTygpGroupItems>  tts = ttg.getTtgItems();
            if(!tts.isEmpty() && tts != null){
                for (TimeTygpGroupItems tt : tts){
                    tygpGroupItemsDao.u(tt);
                }
            }
        }
    }
     **/


}
